package com.tjbklx1.redis.me;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisSet {

	/**
	 * 设置集合的多个值，放入Set集合
	 * 
	 * @param key
	 * @param value
	 */
	public static void sadd(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.sadd(key, value);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
	}
	
	/**
	 * 获取集合类的值
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Set getSmembers(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Set value = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			value = jedis.smembers(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}

		return value;
	}
	
	/**
	 * 对特定setName的set增加一个或多个值，返回是增加元素的个数。 注意：对同一个member多次add，set中只会保留一份。
	 * 
	 * @param setName
	 * @param members
	 * @return
	 */
	public static long sadd(String setName, String... members) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long rank = jedis.sadd(setName, members);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return -1L;
	}

	/**
	 * 判断值是否是set的member。 如果值是set的member返回true，否则，返回false
	 * 
	 * @param setName
	 * @param member
	 * @return
	 */
	public static boolean sismember(String setName, String member) {
		JedisPool pool = null;
		Jedis jedis = null;
		Boolean exists = false;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			exists = jedis.sismember(setName, member);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return exists;
	}

	/**
	 * 移除一个或多个member
	 * 
	 * @param setName
	 * @param members
	 * @return
	 */
	public static long srem(String setName, String... members) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long rank = jedis.srem(setName, members);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return -1L;
	}

	public static Set<String> keys(String pattern) {
		JedisPool pool = null;
		Jedis jedis = null;
		Set<String> value = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			value = jedis.keys(pattern);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return value;
	}
}
